import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/Main.vue'
import Login from '@/Login.vue'
import User from '@/user/index.vue'
import Home from '@/Home.vue'
import Building from '@/Building.vue'
import Map from '@/map.vue'
import test from '@/user/test.vue'

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: '首页',
    component: Main,
    redirect: 'init',
    children: [
      {
        path: '/building',
        name: 'Building',
        component: Building
      },
      {
        path: 'home',
        name: 'Home',
        component: Home
      },
      {
        path: 'user',
        name: 'User',
        component: User
      },
      {
        path: 'test',
        name: 'Test',
        component: test
      },
      {
        path: 'zhidu',
        name: 'Zhidu',
        component: () => import(/* webpackChunkName: "zhidu" */ '@/zhidu/index.vue')
        // component: Zhidu
      },
      {
        path: 'zhidu/import',
        name: 'ZhiduImport',
        component: () => import(/* webpackChunkName: "zhidu" */ '@/Zhidu/import.vue')
        // component: ZhiduImport
      },
      {
        path: 'youngCadres/personlist',
        name: 'youngCadresPersonList',
        component: () => import(/* webpackChunkName: "youngCadres" */ '@/youngCadres/personList.vue')
      },
      {
        path: 'youngCadres/deptlist',
        name: 'youngCadresDeptList',
        component: () => import(/* webpackChunkName: "youngCadres" */ '@/youngCadres/deptList.vue')
      },
      {
        path: 'youngCadres/deptpersonlist',
        name: 'youngCadresDeptPersonList',
        component: () => import(/* webpackChunkName: "youngCadres" */ '@/youngCadres/deptPersonList.vue')
      },
      {
        path: 'youngCadres/log',
        name: 'youngCadresLog',
        component: () => import(/* webpackChunkName: "youngCadresAdmin" */ '@/youngCadres/log.vue')
      },
      {
        path: 'youngCadres/analyse',
        name: 'youngCadresAnalyse',
        component: () => import(/* webpackChunkName: "youngCadres" */ '@/youngCadres/webdatarock.vue')
      },
      {
        path: 'youngCadres/yearlist',
        name: 'youngCadresYearList',
        component: () => import(/* webpackChunkName: "youngCadres" */ '@/youngCadres/yearList.vue')
      },
      {
        path: 'map',
        name: 'Map',
        component: Map
      },
      {
        path: 'password',
        name: 'password',
        component: () => import(/* webpackChunkName: "dept" */ '@/password.vue')
      },
      {
        path: '/recruit',
        name: 'recruit',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/select.vue')
      },
      {
        path: '/recruit/index',
        name: 'recruitIndex',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/index.vue')
      },
      {
        path: '/recruit/main',
        name: 'recruitMain',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/Main.vue')
      },
      {
        path: '/recruit/yearlist',
        name: 'recruitYearList',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/yearList.vue')
      },
      {
        path: '/recruit/unit',
        name: 'recruitUnit',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/Unit.vue')
      },
      {
        path: '/recruit/auth',
        name: 'recruitAuth',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/Auth.vue')
      },
      {
        path: '/recruit/person',
        name: 'recruitPerson',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/Person.vue')
      },
      {
        path: '/recruit/unitview',
        name: 'recruitUnitView',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitView.vue')
      },
      {
        path: '/post',
        name: 'post',
        component: () => import(/* webpackChunkName: "recruit" */ '@/post/select.vue')
      },
      {
        path: '/post/index',
        name: 'postIndex',
        component: () => import(/* webpackChunkName: "recruit" */ '@/post/index.vue')
      },
      {
        path: '/post/main',
        name: 'postMain',
        component: () => import(/* webpackChunkName: "recruit" */ '@/post/Main.vue')
      },
      {
        path: '/post/search',
        name: 'postSearch',
        component: () => import(/* webpackChunkName: "recruit" */ '@/post/search.vue')
      },
      {
        path: '/salary',
        name: 'salaryIndex',
        component: () => import(/* webpackChunkName: "salary" */ '@/salary/index.vue')
      },
      {
        path: '/salary/list',
        name: 'salaryList',
        component: () => import(/* webpackChunkName: "salary" */ '@/salary/List.vue')
      },
      {
        path: '/salary/standardlist',
        name: 'StandardList',
        component: () => import(/* webpackChunkName: "salary" */ '@/salary/StandardList.vue')
      },
      // {
      //   path: '/recruit/main',
      //   name: 'recruitMain',
      //   component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/main.vue')
      // },
      // {
      //   path: '/recruit/Detail',
      //   name: 'recruitDetail',
      //   component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/Detail.vue')
      // }
      {
        path: '/staffing',
        name: 'staffingIndex',
        component: () => import(/* webpackChunkName: "staffing" */ '@/staffing/index.vue')
      },
      {
        path: '/staffing/org',
        name: 'staffingOrg',
        component: () => import(/* webpackChunkName: "staffing" */ '@/staffing/org.vue')
      },
      {
        path: '/staffing/fill',
        name: 'staffingFill',
        component: () => import(/* webpackChunkName: "staffing" */ '@/staffing/fill.vue')
      }
    ]
  },
  {
    name: '404',
    path: '/404',
    component: () => import('@/404.vue')
  },
  {
    path: '/init',
    name: 'init',
    component: () => import(/* webpackChunkName: "dept" */ '@/init.vue')
  },
  {
    path: '/unit',
    name: 'unitmain',
    redirect: '/unit/home',
    component: () => import(/* webpackChunkName: "dept" */ '@/unit/main.vue'),
    children: [
      {
        path: '/unit/home',
        name: 'unithome',
        component: () => import(/* webpackChunkName: "dept" */ '@/unit/home.vue')
      },
      {
        path: '/unit/password',
        name: 'unitpassword',
        component: () => import(/* webpackChunkName: "dept" */ '@/password.vue')
      },
      {
        path: '/unit/youngCadresDept',
        name: 'youngCadresDept',
        component: () => import(/* webpackChunkName: "dept" */ '@/youngCadres/dept.vue')
      },
      {
        path: '/recruit/unit/projectList',
        name: 'recruitUnitProjectList',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitlist.vue')
      },
      {
        path: '/recruit/unit/projectDetail',
        name: 'recruitUnitDetail',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitDetail.vue')
      },
      {
        path: '/recruit/unit/baseInfo',
        name: 'recruitUnitBaseInfo',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitBaseInfo.vue')
      },
      {
        path: '/recruit/unit/person',
        name: 'recruitUnitPerson',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitPerson.vue')
      },
      {
        path: '/recruit/unit/audit',
        name: 'recruitUnitAudit',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitAudit.vue')
      },
      {
        path: '/recruit/unit/auditdetail',
        name: 'recruitUnitAuditDetail',
        component: () => import(/* webpackChunkName: "recruit" */ '@/recruit/unitAuditDetail.vue')
      },
      {
        path: '/post/unit/main',
        name: 'postUnitMain',
        component: () => import(/* webpackChunkName: "recruit" */ '@/post/unitMain.vue')
      },
      {
        path: '/salary/unit/main',
        name: 'salaryUnitMain',
        component: () => import(/* webpackChunkName: "recruit" */ '@/salary/unitMain.vue')
      },
      {
        path: '/staffing/org',
        name: 'staffingUnitOrg',
        component: () => import(/* webpackChunkName: "staffing" */ '@/staffing/org.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '*', // 此处需特别注意至于最底部
    redirect: '/404'
  }
]
// 页面刷新时，重新赋值token
// if (localStorage.getItem('token')) {
//   var params = {data: localStorage.getItem('token'), user: localStorage.getItem('user'), Roles: localStorage.getItem('Roles'), pwdFlag: localStorage.getItem('pwdFlag'), unitName: localStorage.getItem('unitName')}
//   store.commit(types.LOGIN, params)
// }
console.log('router加载')
const router = new Router({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  console.log('from.name')
  console.log(from.name)
  console.log('to.name')
  console.log(to.name)
  // alert('fromname:' + from.name + ',to.name:' + to.name)
  if (to.name !== 'login') { // to.matched.some(r => r.meta.requireAuth)
    if (localStorage.getItem('token')) {
      if (localStorage.getItem('Roles')) {
        if (localStorage.getItem('Roles').indexOf('ROLE_other') > -1) {
          let pathArr = ['init', 'unitmain', 'unitpassword', 'unithome', 'youngCadresDept', 'recruitUnitProjectList', 'recruitUnitDetail', 'recruitUnitAudit', 'recruitUnitBaseInfo', 'recruitUnitPerson', 'recruitUnitAuditDetail', 'postUnitMain', 'salaryUnitMain', 'staffingUnitOrg']
          if (pathArr.indexOf(to.name) > -1) {
            if (JSON.parse(localStorage.getItem('pwdFlag')) || to.name === 'unitpassword') {
              next()
            } else {
              next({
                name: 'unitpassword'
              })
            }
          } else {
            localStorage.clear()
            next({
              path: '/login',
              query: { redirect: to.fullPath }
            })
          }
        } else {
          if (JSON.parse(localStorage.getItem('pwdFlag')) || to.name === 'password') {
            next()
          } else {
            next({
              name: 'password'
            })
          }
        }
      } else {
        next({
          path: '/login',
          query: { redirect: to.fullPath }
        })
      }
    } else {
      // next()
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

export default router
